import Vue from 'vue'
import VueRouter from 'vue-router'
//解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上平凡点击菜单报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location){
  return originalPush.call(this,location).catch(err=>err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component:() => import('../views/Manager.vue'),
    redirect:'/home',//重定向到主页
    children:[
      {path: '/403', name:'Auth',meta:{name:'无权限'}, component:() => import('../views/manager/Auth')},
      {path: 'home',name:'Home',meta:{name:'系统首页'},component: ()=> import('../views/manager/HomeView')},
      {path: 'user',name:'  User',meta:{name:'用户信息'},component: ()=> import('../views/manager/User')},
      {path: 'person',name:'  Person',meta:{name:'个人信息'},component: ()=> import('../views/manager/Person')},
      {path: 'password',name:'  Password',meta:{name:'修改密码'},component: ()=> import('../views/manager/Password')},
    ]
  },
  {
    path: '/element',
    name: 'Element',
    component:() => import('../views/Element.vue')
  },
  {path: '/login', name:'login',meta:{name:'登录'}, component:() => import('../views/Login.vue')},
  {path: '/register', name:'register',meta:{name:'注册'}, component:() => import('../views/Register.vue')},
  {path: '*', name:'404',meta:{name:'无法访问'}, component:() => import('../views/404.vue')},
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫：解决用户页面权限问题
router.beforeEach((to,form,next)=>{
  //to是到达的路由信息
  //from是开源的路由信息
  //next是帮助我们跳转路由的函数
  let adminPaths=['/user']
  let user=JSON.parse(localStorage.getItem("honey-user")||'{}')

  if(user.role!=='管理员'&&!adminPaths.includes(to.path)){
    //如果当前的用户不是管理员，然后当前到达的路径是管理员才有权限访问的路径，那这个时候我就让用户去到一个没有权限的页面，不让它访问实际的页面
    next();
  }else{
    next();
  }
  // if(to.path==='/login'){
  //   next();
  // }
  // const user=localStorage.getItem("honey-user");
  // if(!user&&to.path!=='/login'){
  //   return next('/login');
  // }
  // next();
})

export default router
